﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FileManagerGlobal_Library;
using FileManager_Client.BusinessLogic.FileManagerUserService;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class UpdateInfomation : BaseForm
    {
        private UpdateFileInfo _updateFileInfo;
        public UpdateInfomation(UpdateFileInfo updateFileInfo)
        {
            InitializeComponent();
            _updateFileInfo = updateFileInfo;
            Lb_CurrentVersion.Text += Assembly.GetExecutingAssembly().GetName().Version;
            Lb_ServerVersion.Text += _updateFileInfo.ApplicationVersion;
            if (null != _updateFileInfo.UpdateInfomation && _updateFileInfo.UpdateInfomation.Length > 0)
                Rtb_Infomation.Text = Encoding.UTF8.GetString(_updateFileInfo.UpdateInfomation);
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            var start_update_path = Path.Combine(Application.StartupPath, GlobalVariable.UpdateApplicationFolder);
            start_update_path = Path.Combine(start_update_path, GlobalVariable.UpdateApplicationName);
            var currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            var processStartInfo = new ProcessStartInfo(start_update_path, "\"" + currentVersion + "\"")
                                       {
                                           WorkingDirectory = Path.Combine(Application.StartupPath,
                                                                           GlobalVariable.UpdateApplicationFolder)
                                       };
            Process.Start(processStartInfo);
            Environment.Exit(Environment.ExitCode);
        }

        private void UpdateInfomation_FormClosing(object sender, FormClosingEventArgs e)
        {
            //XtraMessageBox.Show("客户端程序需要更新，请勿关闭，单击确定开始更新!", "更新提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //e.Cancel = true;
            Environment.Exit(Environment.ExitCode);
        }
    }
}